package com.android.videotest.view;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.android.videotest.R;

/**
 * 项目名称：VideoTest
 * 类 名 称：YZVideoView
 * 类 描 述：YZVideoView
 * 创建时间：2019-11-11 15:24
 * 创 建 人：sansecy
 */
public class YZVideoView extends ConstraintLayout {

    private Context mContext;
    private VideoView mVideoView;

    public YZVideoView(Context context) {
        this(context, null);
    }

    public YZVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.yz_video_view, null);
        MediaController mediaController = findViewById(R.id.mediaController);
        mVideoView = view.findViewById(R.id.videoView);
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {

            }
        });
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {

            }
        });
        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
        mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
        mVideoView.setMediaController(mediaController);
    }

    public void loadUrl(String url) {
        mVideoView.setVideoPath(url);
    }

    public void onStart() {
        if (mVideoView != null) {
//            mVideoView.start();
        }
    }

    public void onPause() {
        if (mVideoView != null) {
            mVideoView.pause();
        }
    }

    public void onDestory() {
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
    }
}
